		.CHIP	R65c02
		SYMBOLS	
		LINKLIST
		DEBUG	ASM
		LLCHAR	?
		.syntax	6502


struct_section:	.section
		.absolute
zero:		ds	1
one:		ds	1
plus_value1:	ds	1
plus_value2:	ds	1
eight0:		ds	1
neg_value1:	ds	1
neg_value2:	ds	1
struct_size:	ds	0
		.relative

		PAGE0
		org	40h
value:		ds	struct_size	
error_count:	db	0
err_cnt_init:	db	0
scratch1:	db	ffh

addr_tbl:	.word	data_table
		.word	data_table+1
		.word	data_table+2
		.word	data_table+3
		.word	data_table+4
		.word	data_table+5
		.word	data_table+6
msg:		db 	0,0,0,0,0,0,0,0

		CODE
		org	200h
start:		ldx	#ffh			;set system stack pointer
                txs	
		stz	value
		lda	#01
		sta	value+one
		lda	#3fh
		sta	value+plus_value1	
		lda	#7fh
		sta	value+plus_value2	
		lda	#80h
		sta	value+eight0	
		lda	#afh
		sta	value+neg_value1	
		lda	#ffh
		sta	value+neg_value2	

		sec				;set carry
		bcs	adc
		jsr	error			

adc:		lda	#00h			;set zero, clear carry
		beq	?0
		jsr	error
?0:		clc	
		ldx	#00h
		adc	value,x
		beq	?1
		jsr	error
?1:		clc				;clear carry	
		adc	#-1			;set N
		bmi	?2
		jsr	error
?2:		adc	value+plus_value1	;clear N
		bpl	?3
		jsr	error
?3:		adc	value+plus_value2	;set V
		bvs	?4
		jsr	error
?4:		adc	data_table+one		;clear V
		bvc	?5
		jsr	error
?5:		adc	value+neg_value2	;set carry
		bcs	?6
		jsr	error
?6:		adc	data_table+plus_value1	;clear carry
		bcc	?7
		jsr	error
?7		lda	#00h			;clear a	
		ldx	#0ch			;initialize x
		ldy	#06h			;initialize y
?8:		adc	(addr_tbl,x)
		adc	(addr_tbl),y
		dex
		dex
		dey
		bpl	?8
		cmp	#dfh
		beq	?9	
		jsr	error
?9:		clc	
		adc	(addr_tbl)
		cmp	#dfh
		beq	and	
		jsr	error
		
and:		and	#00h			;set zero
		beq	?0
		jsr	error
?0:		ldx	#00h	
		and	value,x
		beq	?1
		jsr	error
?1:		lda	#80h
		and	value+neg_value2	;set sign
		bmi	?2
		jsr	error
?2:		lda	#ffh			;clear negative	
		ldx	#0ch			;load offset	
		ldy	#01h
		and	(addr_tbl,x)
		and	(addr_tbl),y
		bpl	?3
		jsr	error
?3:		and	data_table+plus_value1	;clear carry
		bne	?4
		jsr	error
?4:		and	data_table,y
		bne	?5
		jsr	error
?5:		ldx	#neg_value2	
		and	data_table,x
		bne	?6	
		jsr	error
?6:		and	(addr_tbl)
		beq	asl
		jsr	error
asl:		lda	#ffh	
                clc
		asl	a
		cmp	#feh
		beq	?1	
		jsr	error
?1:		asl	a
		bcs	?2	
		jsr	error
?2:		lda	value+zero
		sta	scratch1
		asl	scratch1
		bcc	?3
		jsr	error
?3:		lda	value+neg_value2
		sta	scratch2
		asl	scratch2
		bmi	?4
		jsr	error
?4:		ldx	#zero
		asl	value,x
		beq	?5
		jsr	error
?5:		asl	data_table,x
		bpl	?6	
		jsr	error
?6:		asl	value,x
		bpl	bbr
		jsr	error
bbr:		bbr	0,value,?1
		jsr	error
?1:		bbs	1,value+neg_value2,?2
		jsr	error
?2:		bbr	2,value,?3
		jsr	error
?3:		bbs	3,value+neg_value2,?4
		jsr	error
?4:		bbr	4,value,?5
		jsr	error
?5:		bbs	5,value+neg_value2,?6
		jsr	error
?6:		bbr	6,value,?7
		jsr	error
?7:		bbs	7,value+neg_value2,?8
		jsr	error
?8:		bbs	0,value+neg_value2,?9
		jsr	error
?9:		bbr	1,value,?10
		jsr	error
?10:		bbs	2,value+neg_value2,?11
		jsr	error
?11:		bbr	3,value,?12
		jsr	error
?12:		bbs	4,value+neg_value2,?13
		jsr	error
?13:		bbr	5,value,?14
		jsr	error
?14:		bbs	6,value+neg_value2,?15
		jsr	error
?15:		bbr	7,value,bit
	        jsr	error
bit:		lda	#ffh
		bit	value+zero		;set zero
                beq	?1
		jsr	error
?1:		bit	value+plus_value2	;clear zero
		bne	?2
		jsr	error
?2:		ldx	#00h
		bit	value,x			;set zero	
		beq	?3
		jsr	error
?3:		inx				;clear zero
		bit	data_table,x
		bne	?4
		jsr	error
?4:		bit	#00h			;set zero
		beq	clc
		jsr	error
clc:		sec	
		clc
		bcc	clv	
		jsr	error
clv:		clv
		bvc	cmp
		jsr	error
cmp:		clc				;clear carry
		ldy	#00h			;clear sign flag
		lda	value+neg_value2	;load a with ffh
		cmp	#00			;compare 0xff to 0	
		bcs	?1
		jsr	error
?1:		bmi	?2
		jsr	error
?2:		bne	?3
		jsr	error
?3:		clc	
		ldy	#00h			;clear sign flag
		cmp	value
		bcs	?4
		jsr	error
?4:		bmi	?5
		jsr	error
?5:		bne	?6
		jsr	error
?6:		ldx	#neg_value2
		clc	
		ldy	#00h			;clear sign flag
		cmp	value,x
		bcs	?7
		jsr	error
?7:		bpl	?8
		jsr	error
?8:		beq	?9
		jsr	error
?9:		clc	
		ldy	#00h			;clear sign flag
		cmp	data_table					
		bcs	?10
		jsr	error
?10:		bmi	?11
		jsr	error
?11:		bne	?12
		jsr	error
?12:		clc	
		ldy	#00h			;clear sign flag
		cmp	data_table,x
		bcs	?13
		jsr	error
?13:		bpl	?14
		jsr	error
?14:		beq	?15
		jsr	error
?15:		ldy	#00h
		clc	
		ldy	#00h			;clear sign flag
		cmp	data_table,y
		bcs	?16
		jsr	error
?16:		bmi	?17
		jsr	error
?17:		bne	?18
		jsr	error
?18:		clc	
		ldx	#neg_value2
		ldy	#00h			;clear sign flag
		cmp	(addr_tbl,x)
		bcs	?19
		jsr	error
?19:		bmi	?20
		jsr	error
?20:		bne	?21
		jsr	error
?21:		ldy	#00h			;clear sign flag
		cmp	(addr_tbl),y
		bcs	?22
		jsr	error
?22:		bmi	?23
		jsr	error
?23:		bne	?24
		jsr	error
?24:		clc	
		ldy	#00h			;clear sign flag
		cmp	(addr_tbl)
		bcs	?25
		jsr	error
?25:		bmi	?26
		jsr	error
?26:		bne	cpx	
		jsr	error
cpx:		ldy	#00h			;clear sign flag
		ldx	value
		clc				;clear carry
		cpx	#00
		bcs	?1
		jsr	error
?1:		bpl	?2
		jsr	error
?2:		beq	?3
		jsr	error
?3: 		cpx	value+neg_value1
		bcc	?4
		jsr	error
?4:		bpl	?5
		jsr	error
?5:		bne	?6
		jsr	error
?6:		cpx	data_table+zero
		bcs	?7
		jsr	error
?7:		bpl	?8
		jsr	error
?8:		beq	cpy	
		jsr	error
cpy:		ldy	#00h			;clear sign flag
		ldx	value
		clc				;clear carry
		cpy	#00
		bcs	?1
		jsr	error
?1:		bpl	?2
		jsr	error
?2:		beq	?3
		jsr	error
?3: 		cpy	value+neg_value1
		bcc	?4
		jsr	error
?4:		bpl	?5
		jsr	error
?5:		bne	?6
		jsr	error
?6:		cpy	data_table+zero
		bcs	?7
		jsr	error
?7:		bpl	?8
		jsr	error
?8:		beq	dec	
		jsr	error
dec:		lda	value
		sta	scratch1
		dec	scratch1
		bne	?1
		jsr	error
?1:		bmi	?2
		jsr	error
?2:		ldx	#one
		dec	value,x	
		beq	?3
		jsr	error
?3:		bpl	?4
		jsr	error
?4:		lda	#01h
		sta	value+one
		lda	data_table,x
		sta	scratch2
		dec	scratch2
		beq	?5
		jsr	error
?5:		bpl	?6
		jsr	error
?6:		ldx	#neg_value2
		dec	data_table,x
		bne	?7
		jsr	error
?7:		bmi	?8
		jsr	error
?8:		lda	value+neg_value2
		sta	data_table+neg_value2
		dec	a
		bmi	?9
		jsr	error
?9:		bne	dex	
		jsr	error
dex:		dex
		bpl	?1
		jsr	error
?1:		bne	dey
		jsr	error
dey:		ldy	#ffh	
		dey
		bmi	?1
		jsr	error
?1:		bne	eor
		jsr	error
eor:		lda	data_table,x
		eor	#afh
		beq	?1
		jsr	error
?1:		bpl	?2
		jsr	error
?2:		lda	value+one
		eor	value+neg_value2
		bmi	?3
		jsr	error
?3:		bne	?4
		jsr	error
?4:		lda	value
		ldx	#zero
		eor	value,x
		beq	?5
		jsr	error
?5:		bpl	?6
		jsr	error
?6:		eor	data_table+neg_value2
		bmi	?7
		jsr	error
?7:		bne	?8
		jsr	error
?8:		ldx	#neg_value2
		eor	data_table,x
		beq	?9
		jsr	error
?9:		bpl	?10
		jsr	error
?10:		ldy	#neg_value1
		eor	data_table,y
		bmi	?11
		jsr	error
?11:		bne	?12
		jsr	error
?12:		ldx	#neg_value1*2
		eor	(addr_tbl,x)
		beq	?13
		jsr	error
?13:		bpl	?14
		jsr	error
?14:		eor	(addr_tbl),y
		bmi	?15
		jsr	error
?15:		bne	?16
		jsr	error
?16:		eor	(addr_tbl+(neg_value1*2))
		beq	?17
		jsr	error
?17:		bpl	inc
		jsr	error
inc:		lda	data_table+neg_value2
		inc	a
		beq	?1
		jsr	error
?1:		bpl	?2
		jsr	error
?2:		lda	data_table+one
		sta	scratch1
		inc	scratch1
		bne	?3
		jsr	error
?3:		bpl	?4
		jsr	error
?4:		ldx	#neg_value2
		inc	value,x
		beq	?5
		jsr	error
?5:		bpl	?6
		jsr	error
?6:		lda	data_table+neg_value2
		sta	value+neg_value2
		sta	scratch2
		inc	scratch2
		beq	?7
		jsr	error
?7:		bpl	?8
		jsr	error
?8:		ldx	#plus_value2
		inc	data_table,x
		bmi	?9
		jsr	error
?9:		bne	?10
		jsr	error
?10:		lda	value+plus_value2
		sta	data_table,x
inx:		inx	
		bpl	?1
		jsr	error
?1:		bne	iny
		jsr	error
iny:		iny
		bpl	?1
		jsr	error
?1:		bne	jmp	
		jsr	error
jmp:		jmp	absolute	
		nop
		nop
return:		nop
jsr:		jsr	subr
lda:		lda	#ffh
		bne	?1
		jsr	error
?1:		bmi	?2
		jsr	error
?2:		lda	value
		beq	?3
		jsr	error
?3:		bpl	?4
		jsr	error
?4:		ldx	#neg_value1
		lda	value,x
		bne	?5
		jsr	error
?5:		bmi	?6
		jsr	error
?6:		lda	data_table
		beq	?7
		jsr	error
?7:		bpl	?8
		jsr	error
?8:		lda	data_table,x
		bne	?9
		jsr	error
?9:		bmi	?10
		jsr	error
?10:		ldy	#one
		lda	data_table,y
		bpl	?11
		jsr	error
?11:		bne	?12
		jsr	error
?12:		ldx	#neg_value2*2
		lda	(addr_tbl,x)
		bmi	?13
		jsr	error
?13:		bne	?14
		jsr	error
?14:		dey		
		lda	(addr_tbl),y
		beq	?15
		jsr	error
?15:		bpl	?16
		jsr	error
?16:		lda	#ffh		;set sign and clear zero
		lda	(addr_tbl)
		beq	?17
		jsr	error
?17:		bpl	ldx
		jsr	error
ldx:		ldx	#ffh
		bne	?1
		jsr	error
?1:		bmi	?2
		jsr	error
?2:		ldx	value
		beq	?3
		jsr	error
?3:		bpl	?4
		jsr	error
?4:		ldy	#neg_value1
		ldx	value,y
		bne	?5
		jsr	error
?5:		bmi	?6
		jsr	error
?6:		ldx	data_table
		beq	?7
		jsr	error
?7:		bpl	?8
		jsr	error
?8:		ldx	data_table,y
		bne	?9
		jsr	error
?9:		bmi	ldy	
		jsr	error
ldy:		ldy	#ffh
		bne	?1
		jsr	error
?1:		bmi	?2
		jsr	error
?2:		ldy	value
		beq	?3
		jsr	error
?3:		bpl	?4
		jsr	error
?4:		ldx	#neg_value1
		ldy	value,x
		bne	?5
		jsr	error
?5:		bmi	?6
		jsr	error
?6:		ldy	data_table
		beq	?7
		jsr	error
?7:		bpl	?8
		jsr	error
?8:		ldy	data_table,x
		bne	?9
		jsr	error
?9:		bmi	lsr	
		jsr	error
lsr:		clc	
		lda	#ffh
		lsr	a
		bpl	?1
		jsr	error
?1:		bne	?2
		jsr	error
?2:		bcs	?3
		jsr	error
?3:		cmp	value+plus_value2
		beq	?4
		jsr	error
?4:		lda	value
		sta	scratch1
		lsr	scratch1		
		beq	?5
		jsr	error
?5:		bpl	?6
		jsr	error
?6:		bcc	?7
		jsr	error
?7:		ldx	#neg_value2
		lsr	value,x
		bne	?8
		jsr	error
?8:		bpl	?9
		jsr	error
?9:		bcs	?10
		jsr	error
?10:		lda	#ffh			;restore value+neg_value2
		sta	value,x
		lda	data_table
		sta	scratch2
		lsr	scratch2
		beq	?11
		jsr	error
?11:		bpl	?12
		jsr	error
?12:		bcc	?13
		jsr	error
?13:		lsr	data_table,x
		bne	?14
		jsr	error
?14:		bpl	?15
		jsr	error
?15:		bcs	?16
		jsr	error
?16:		lda	#ffh
		sta	data_table,x
ora:		ora	#0
		bmi	?1
		jsr	error
?1:		bne	?2
		jsr	error
?2:		lda	#0
		ora	value
		beq	?3
		jsr	error
?3:		bpl	?4
		jsr	error
?4:		ldx	#one
		ora	value,x
		bne	?5
		jsr	error
?5:		bpl	?6
		jsr	error
?6:		ora	data_table
		cmp	#1
		beq	?7
		jsr	error
?7:		ora	data_table,x
		cmp	#1
		beq	?8
		jsr	error
?8:		ldy	#neg_value2	
               	ora	data_table,y
		bmi	?9
		jsr	error
?9:		bne	?10
		jsr	error
?10:		ldx	#one*2
		lda	data_table
		ora	(addr_tbl,x)
		cmp	#1
		beq	?11
		jsr	error
?11:		ora	(addr_tbl),y
		cmp	#ffh
		beq	?12
		jsr	error
?12:		lda	value
		ora	(addr_tbl)
		cmp	#0
		beq	?13
		jsr	error
?13:		clc
pha:		pha
php:		php
phx:		phx
phy:		phy
		ldx	#ffh
		ldy	#ffh
ply:		ply
plx:		plx
plp:		plp
		lda	#ffh	
pla:		pla
rmb:		rmb	0,value+neg_value2
		rmb	1,value+neg_value2
		rmb	2,value+neg_value2
		rmb	3,value+neg_value2
		rmb	4,value+neg_value2
		rmb	5,value+neg_value2
		rmb	6,value+neg_value2
		rmb	7,value+neg_value2
		lda	value+neg_value2
		beq	?1	
		jsr	error
?1:		lda	#ffh
		sta	value+neg_value2
		clc
		ldx	#0
rol:		rol	a
		bmi	?1
		jsr	error
?1:		bne	?2
		jsr	error
?2:		bcs	?3
		jsr	error
?3:		lda	value+plus_value2
		sta	scratch1
		rol	scratch1
		bne	?4
		jsr	error
?4:		bmi	?5
		jsr	error
?5:		bcc	?6
		jsr	error
?6:		rol	scratch1,x
		bcs	?7
		jsr	error
?7:		bmi	?8
		jsr	error
?8:		bne	?9
		jsr	error
?9:		rol	data_table
		bcc	?10
		jsr	error
?10:		bpl	?11
		jsr	error
?11:		bne	?12
		jsr	error
?12:		sec	
		stz	data_table
		ldy	#plus_value2
		lda	data_table,y
		sta	scratch2
		rol	scratch2,x
		bcc	?13
		jsr	error
?13:		bmi	?14
		jsr	error
?14:		bne	ror
		jsr	error
ror:		ror	a
		bcs	?1
		jsr	error
?1:		bpl	?2
		jsr	error
?2:		bne	?3
		jsr	error
?3:		lda	value+plus_value2
		clc
		sta	scratch1
		ror	scratch1
		bne	?4
		jsr	error
?4:		bpl	?5
		jsr	error
?5:		bcs	?6
		jsr	error
?6:		clc		
		ror	scratch1,x
		bcs	?7
		jsr	error
?7:		bpl	?8
		jsr	error
?8:		bne	?9
		jsr	error
?9:		ror	data_table
		bcc	?10
		jsr	error
?10:		bmi	?11
		jsr	error
?11:		bne	?12
		jsr	error
?12:		sec	
		stz	data_table
		ldy	#plus_value2
		lda	data_table,y
		sta	scratch2
		clc
		ror	scratch2,x
		bcs	?13
		jsr	error
?13:		bpl	?14
		jsr	error
?14:		bne	sbc	
		jsr	error
sbc:		lda	#0
		clc
		sbc	#0
		bcc	?1
		jsr	error
?1:		bmi	?2
		jsr	error
?2:		bne	?3
		jsr	error
?3:		bvc	?4
		jsr	error
?4:		sbc	value+eight0
		bcs	?5
		jsr	error
?5:		bpl	?6
		jsr	error
?6:		bvc	?7
		jsr	error
?7:		bne	?8
		jsr	error
?8:		ldx	#plus_value2
		sbc	value,x
		bcc	?9
		jsr	error
?9:		bvc	?10
		jsr	error
?10:		bmi	?11
		jsr	error
?11:		bne	?12
		jsr	error
?12:		sbc	data_table
		bcs	?13
		jsr	error
?13:		bvc	?14
		jsr	error
?14:		bmi	?15
		jsr	error
?15:		bne	?16
		jsr	error
?16:		sbc	data_table,x
		bvs	?17
		jsr	error
?17:		bcs	?18
		jsr	error
?18:		bpl	?19
		jsr	error
?19:		bne	?20
		jsr	error
?20:		sbc	data_table,y
		bcs	?21
		jsr	error
?21:		bvc	?22
		jsr	error
?22:		beq	?23
		jsr	error
?23:		bpl	?24
		jsr	error
?24:		inx	
		sbc	(addr_tbl,x)
		bcc	?25
		jsr	error
?25:		bmi	?26
		jsr	error
?26:		bvc	?27
		jsr	error
?27:		bne	?28
		jsr	error
?28:		sbc	(addr_tbl),y
		bvs	?29
		jsr	error
?29:		bcs	?30
		jsr	error
?30:		bpl	?31
		jsr	error
?31:		bne	sec
		jsr	error
sec:		clc	
		sec
		bcs	sed
		jsr	error
sed:		cld	
		sed
		cld
sei:		cli	
		sei
		clv
smb:		smb	0,value+zero
		smb	1,value+zero
		smb	2,value+zero
		smb	3,value+zero
		smb	4,value+zero
		smb	5,value+zero
		smb	6,value+zero
		smb	7,value+zero
		lda	value+zero
		inc	a
		beq	?1	
		jsr	error
?1:		sta	value+zero
sta:		lda	value+zero
		beq	?1
		jsr	error
?1:		sta	value,x
		lda	value,x
		beq	?2
		jsr	error
?2:		sta	data_table
		lda	data_table
		beq	?3
		jsr	error
?3:		sta	data_table,x
		lda	data_table,x
		beq	?4
		jsr	error
?4:		sta	data_table,y
		lda	data_table,y
		beq	?5
		jsr	error
?5:		sta	(addr_tbl,x)
		lda	(addr_tbl,x)
		beq	?6
		jsr	error
?6:		sta	(addr_tbl),y
		lda	(addr_tbl),y
		beq	?7
		jsr	error
?7:		sta	(addr_tbl)
		lda	(addr_tbl)
		beq	stx
		jsr	error
stx:		ldx	#ffh
		stx	value
		ldx	value
		bmi	?1
		jsr	error
?1:		stx	value,y
		ldx	value,y
		bmi	?2
		jsr	error
?2:		stx	data_table
		lda	data_table
		bmi	sty
		jsr	error
sty:		ldx	#plus_value2	
		sty	value
		lda	value
		cmp	value
		beq	?1
		jsr	error
?1:		sty	value,x
		lda	value,x
		cmp	value,x
		beq	?2
		jsr	error
?2:		sty	data_table
		lda	data_table
		cmp	data_table
		beq	stz
		jsr	error
stz:		stz	value
		lda	value
		beq	?1
		jsr	error
?1:		stz	value,x	
		lda	value,x
		beq	?2
		jsr	error
?2:		stz	data_table
		lda	data_table
		beq	?3
		jsr	error
?3:		stz	data_table,x
		lda	data_table,x
		beq	tax
		jsr	error
tax:		tax
		beq	?1
		jsr	error
?1:		bpl	tay
		jsr	error
tay:		tay
		beq	?1
		jsr	error
?1:		bpl	trb
		jsr	error
trb:		lda	#0fh
		sta	scratch1
		trb	scratch1
		bne	?1
		jsr	error
?1:		pha
		lda	#f0h
		sta	scratch2
		pla
		trb	scratch2
		beq	tsb
		jsr	error
tsb:		lda	#f0h
		sta	scratch1
		tsb	scratch1
		bne	?1
		jsr	error
?1: 		sta	scratch2
		lda	#0fh
		tsb	scratch2
		beq	tsx
		jsr	error
tsx:		tsx
txa:		txa
		inx
txs:		txs
tya:		tya	
	

done:		lda	error_count
		ldx	#0
		cmp	#0
		bne	fail_loop
pass_loop:	lda	pass_msg,x
		sta	msg,x
		inx
		cpx	#6
		bne	pass_loop
		jmp	done1    
fail_loop:	lda	fail_msg,x
		sta 	msg,x
		inx
		cpx	#6
		bne	fail_loop
done1:		nop
;tst1:		jmp	($1000,x)
tst1:		jmp	($1080,x)
tst2:		jmp	($1000, x)
tst3:		jmp	($1000,x)
tst4:		jmp	($1000, x)
absolute:	jmp	(indirect_addr)
subr:		nop
		nop
		nop
		jsr	subr1
		nop
		nop
		rts
subr1:		nop
		nop
		nop
		jsr	subr2
		nop
		nop
		rts
subr2:		nop
		nop
		nop
		nop
		rts


		DATA
		org	100h
sys_stk:	ds	ffh	
sys_stk_end:	ds	0

		org	e000h

data_table:	db	00h
		db	01h
		db	3fh
		db	7fh
		db	80h
		db	afh
		db	ffh	

scratch2:	db	ffh
indirect_addr:	dw	return
pass_msg:	db	"Passed"
fail_msg:	db	"Failed"

		CODE
		org	ff00h
error:		inc	error_count
		rts

reset:		section
		.org	fffch
		dw	start	
		dw	start	
;
		end	start

		




